// components/AttachmentFiles.js
const APP = getApp();
const TAB_DATA = [
    {label:'图片',value:'image',num:0,},
    {label:'视频',value:'video',num:0,},
    {label:'音频',value:'audio',num:0,},
    {label:'文档',value:'document',num:0,},
];

Component({
    lifetimes: {
        attached: function() {
            // 在组件实例进入页面节点树时执行
            
            //【在data中设置回调方法】
            let _this = this;
            this.data.setAttachmentFiles = function(list){
                for(let item of list){
                  switch(item.fileType){
                    case 'image':
                        _this.data.fileObj.images.push({
                            originalname:item.fileName,
                            urlname:item.fileUrl,
                            pathname:item.filePath,
                            presentname:item.presentName,
                            type:'image',
                        });
                        break;
                    case 'video':
                        _this.data.fileObj.videos.push({
                            originalname:item.fileName,
                            urlname:item.fileUrl,
                            pathname:item.filePath,
                            presentname:item.presentName,
                            type:'video',
                        });
                        break;
                    case 'audio':
                        _this.data.fileObj.audios.push({
                            originalname:item.fileName,
                            urlname:item.fileUrl,
                            pathname:item.filePath,
                            presentname:item.presentName,
                            type:'audio',
                        });
                        break;
                    case 'document':
                        _this.data.fileObj.documents.push({
                            originalname:item.fileName,
                            urlname:item.fileUrl,
                            pathname:item.filePath,
                            presentname:item.presentName,
                            type:'document',
                        });
                        break;
                  }
                }
                
                //Tabs标签设置
                let tmpTabs = TAB_DATA.filter((item)=>{
                    item.num = _this.data.fileObj[item.value+'s'].length;
                    return _this.data.uploadType.includes(item.value);
                });
                _this.setData({
                    uploadTabs:tmpTabs,
                    ['fileObj.images']:_this.data.fileObj.images,
                    ['fileObj.videos']:_this.data.fileObj.videos,
                    ['fileObj.audios']:_this.data.fileObj.audios,
                    ['fileObj.documents']:_this.data.fileObj.documents,
                },function(){
                    _this.doLocation();
                });
                APP.applog.info(_this.data.fileObj);
            },
            //绑定事件： bindSynchUploadReffer
            this.triggerEvent('SynchUploadReffer',this); //【注意】事件名称区分大小写
        },
        ready:function(){
            // 在组件在视图层布局完成后执行
            let _this = this;
            //Tabs标签设置
            let tmpTabs = TAB_DATA.filter((item)=>{
                item.num = _this.data.fileObj[item.value+'s'].length;
                return _this.data.uploadType.includes(item.value);
            });
            this.setData({
                uploadTabs:tmpTabs,
            },function(){
                _this.doLocation();
            });
        },
        detached: function() {
          // 在组件实例被从页面节点树移除时执行
          for(let filepath of this.data.tmpDownloadFilePath){
             APP.apptool.unlinkDocument(filepath);
          }
        },
    },
    /**
     * 组件的属性列表
     */
    properties: {
        //附件列表
        fileObj:{
            type:Object,
            value:{images:[],videos:[],audios:[],documents:[]},
        },
        //上传类型
        uploadType:{
            type:Array,
            // value:['image','video','audio','document']
            value:['image','video','document']
        },
        //默认上传
        defaultType:{
            type:String,
            value:'image',
        },
        //默认索引
        uploadIndex:{
            type:Number,
            value:0,
        },
        //布局类型
        layoutType:{
            type:Number,
            value:1,//1》同上传；2》列表式；
        },
        //标头文本
        headerText:{
            type:String,
            value:'相关附件',
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        maxFileNum:0,//不显示上传功能
        uploadTabs:[],
        tmpDownloadFilePath:[],//临时缓存路径
        isEmptyData:true,//空数据标识（默认）
    },

    /**
     * 组件的方法列表
     */
    methods: {
        //预览
        bindPreview(e){
            let _this = this;
            let dataset = e.currentTarget.dataset;
            let tmpType = (this.data.layoutType == 1?this.data.defaultType:dataset.type);
            if(tmpType == 'image'){
                wx.previewImage({
                    current: dataset.src, // 当前显示图片的 http 链接
                    urls: _this.data.fileObj.images.map((currentValue, index,arr)=>{
                        return currentValue.urlname;
                    }), // 需要预览的图片 http 链接列表
                });
            }else if(tmpType == 'video'){
                wx.previewMedia({                  
                    current:dataset.idx,
                    sources: _this.data.fileObj.videos.map((currentValue, index,arr)=>{
                        return {url:currentValue.urlname,type:'video',poster:''};
                    }),
                });
            }else if(tmpType == 'audio'){
                
            }else if(tmpType == 'document'){
                APP.apptool.openDocument(_this.data.fileObj.documents[dataset.idx].urlname,function(tempFilePath){
                    _this.data.tmpDownloadFilePath.push(tempFilePath);
                });
            }
        },
        //上传类型
        bindTabChange(e){
            this.setData({
                defaultType:e.detail.value
            });
        },
        /**
         * 操作方法
         */
        //定位显示
        doLocation(){
            let _this = this;

            //定位有附件项
            if(_this.data.fileObj['images'].length>0){
                this.data.defaultType = 'image';
                this.data.isEmptyData = false;
            }else if(_this.data.fileObj['videos'].length>0){
                this.data.defaultType = 'video';
                this.data.isEmptyData = false;
            }else if(_this.data.fileObj['audios'].length>0){
                this.data.defaultType = 'audio';
                this.data.isEmptyData = false;
            }else if(_this.data.fileObj['documents'].length>0){
                this.data.defaultType = 'document';
                this.data.isEmptyData = false;
            }else{
                this.data.isEmptyData = true;
            }
            
            this.setData({
                isEmptyData:_this.data.isEmptyData,
                defaultType:_this.data.defaultType,
            });
        }
    },
})
